package com.project.lvshi.controller.aiSeek;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.lvshi.domain.*;
import com.project.lvshi.enums.ResultCode;
import com.project.lvshi.service.*;
import com.project.lvshi.utils.TokenUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail.SimpleEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 咨询controller
 * @date 2025/09/27 09:18
 */
@Controller
@ResponseBody
@RequestMapping("aiSeek")
public class aiSeekController {

    @Autowired
    private AiSeekService sysSeekService;
    @Autowired
    private AiSeekItemService sysSeekItemService;
    @Autowired
    private UserService userService;
    @PostMapping("deleteAiSeekAll")
    @Transactional(rollbackFor = Exception.class)
    public Result removeAiSeekAll() {
        //获取用户id
        String userId = TokenUtils.getUserIdByToken();
        //删除咨询
        LambdaQueryWrapper<AiSeek> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AiSeek::getUserId,userId);
        sysSeekService.remove(queryWrapper);
        //删除咨询内容
        LambdaQueryWrapper<AiSeekItem> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(AiSeekItem::getUserId,userId);
        sysSeekItemService.remove(queryWrapper1);

        return Result.success();
    }

    /** 分页获取咨询 */
    @GetMapping("getAiSeekAll")
    public Result getSysSeekPage() {
        //获取用户id
        String userId = TokenUtils.getUserIdByToken();

        LambdaQueryWrapper<AiSeek> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AiSeek::getUserId,userId);
        List<AiSeek> list = sysSeekService.list(queryWrapper);

        return Result.success(list);
    }

    /** 根据id获取咨询 */
    @GetMapping("getAiSeekById")
    public Result getSysSeekById(@RequestParam("id") String id) {
        AiSeek sysSeek = sysSeekService.getById(id);
        return Result.success(sysSeek);
    }

    /** 保存咨询 */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("saveAiSeek")
    public Result saveSysSeek(@RequestBody AiSeek sysSeek) throws Exception {
        String idStr = IdWorker.getIdStr();
        sysSeek.setId(idStr);
        String id = TokenUtils.getUserIdByToken();
        sysSeek.setUserId(id);
//        User user = userService.getById(sysSeek.getLawyerId());  63344eaa282005cccbe4458e596d5a96
        sysSeek.setLawyerName("deepSeek");
        sysSeek.setAvatar("image/deepseek");
        boolean save = sysSeekService.save(sysSeek);
        //先保存内容
        AiSeekItem sysSeekItem = new AiSeekItem();
        sysSeekItem.setSeekId(idStr);
        sysSeekItem.setContent("您想要咨询的问题“ " + sysSeek.getTitle() + "”,正在思考中，请稍后。");
        sysSeekItem.setUserId(id);
        User user1 = userService.getById(id);
        sysSeekItem.setAvatar(user1.getAvatar());
        sysSeekItemService.save(sysSeekItem);
        if (save) {
            return Result.success(idStr);
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑咨询 */
    @PostMapping("editAiSeek")
    public Result editSysSeek(@RequestBody AiSeek sysSeek) {
        boolean save = sysSeekService.updateById(sysSeek);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除咨询 */
    @GetMapping("removeAiSeek")
    @Transactional(rollbackFor = Exception.class)
    public Result removeSysSeek(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                sysSeekService.removeById(id);
                //删除咨询内容
                QueryWrapper<AiSeekItem> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(AiSeekItem::getSeekId,id);
                sysSeekItemService.remove(queryWrapper);
            }
            return Result.success();
        } else {
            return Result.fail("咨询id不能为空！");
        }
    }

}
